package com.qf.openapi.web.master.interceptor;

import com.qf.openapi.web.master.entity.User;
import com.qf.openapi.web.master.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("拦截到url：{}", request.getRequestURI());
        final HttpSession session = request.getSession();
        final User user = (User) session.getAttribute(Constants.USER_SESSION_KEY);
        if(user==null){
            log.debug("请先登陆");
            response.sendRedirect("/login.html");
            return false;
        }
        log.debug("已放行");
        return true;
    }
}
